<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 告警数据
 *
 * @icon fa fa-circle-o
 */
class Alarmdata extends Backend
{

    /**
     * Alarmdata模型对象
     * @var \app\admin\model\Alarmdata
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Alarmdata;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $sql_id = input('sql_id');
            $sql_info = Db::name('crontab')->where('id', $sql_id)->field('dept_ids, type')->find();

            $where_sql['dept_id'] = intval($sql_info['dept_ids']);
            $where_sql['type'] = $sql_info['type'];
//            dump($where_sql);

            $list = $this->model
                ->with(['dept'])
                ->where($where)
                ->where($where_sql)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','host','name','description','priority','clock','dept_id','dept.name','type']);

            }
            $this->assign('sql_id', $sql_id);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /*
     * 数据分析
     * */
    public function analyze()
    {
        $sql_id = input('sql_id');
        $sql_info = Db::name('crontab')->where('id', $sql_id)->field('dept_ids, type')->find();

        $result = [
            'status' => 6,
            'msg' => '分析完成',
        ];

        if ($sql_info['dept_ids'] == 5 && $sql_info['type'] == 1)
        {
            // 智能家居事业部——业务接口类
            $use = $this->homeBusiness($sql_info['dept_ids']);

            if ($use) {
                return $result;
            } else {
                $result['status'] = 5;
                $result['msg'] = '分析失败,请重试!';

                return $result;
            }
        } elseif ($sql_info['type'] == 1) {
            // 其它部门的业务接口类
            $use = $this->otherOneType($sql_info['dept_ids']);

            if ($use) {
                return $result;
            } else {
                $result['status'] = 5;
                $result['msg'] = '分析失败,请重试!';

                return $result;
            }
        } elseif ($sql_info['type'] == 2) {
            // 服务应用类
            $use = $this->twoThreeType($sql_info['dept_ids'], $sql_info['type']);
            if ($use) {
                return $result;
            } else {
                $result['status'] = 5;
                $result['msg'] = '分析失败,请重试!';

                return $result;
            }
        } elseif ($sql_info['type'] == 3) {
            // 底层服务环境+云分类
            $use = $this->twoThreeType($sql_info['dept_ids'], $sql_info['type']);
            $cloud = $this->cloud($sql_info['dept_ids']);

            if ($use && $cloud) {
                return $result;
            } else {
                $result['status'] = 5;
                $result['msg'] = '分析失败,请重试!';

                return $result;
            }
        }
    }

    /*
     * 智能家居事业部——业务接口类
     * */
    public function homeBusiness($dept_id)
    {
        $data = Db::name('alarm_data')->where("dept_id = $dept_id and type = 1")->column('clock, description');
        $new = [];

        // 数据处理是否完成
        $first = 0;
        $second = 0;
        if ($data) {
            foreach ($data as $k=>&$v)
            {
                // 获取值出现的次数

                // 主机端口或进程 des 二字段
                $ex = explode('-', $v);
                $v = $ex[1].'-'.$ex[2];

                // 21CN 和 云公司 des 一字段
                $data_one[$k] = $ex[0].'-'.$ex[1];

                // 其它 des 二、三字段
                if (count($ex) > 3) {
                    $data_other[$k] = $ex[1].'-'.$ex[2].'~'.$ex[3];
                }

            }
            $host = array_count_values($data);
            $one = array_count_values($data_one);
            $other = array_count_values($data_other);
            // 过滤 主机端口或进程的数据
            $new_host_one = [];
            if ($host) {
                foreach ($host as $k=>&$v)
                {
                    $ex_host = explode('-', $k);
                    if($ex_host[0] == '主机端口或进程')
                    {
                        $new_host[$k] = $v;
                    }
                }
            }

            // 过滤21CN 和 云公司的数据
            if ($one) {
                foreach ($one as $k=>&$v)
                {
                    $ex_host = explode('-', $k);
                    if($ex_host[0] == '21CN' || $ex_host[0] == '云公司')
                    {
                        $new_host_one[$k] = $v;
                    }
                }
            }

            // return $new_host_one;

            // 统计的月份
            $casual = array_keys($data)[0];
            $month = date('Y-m-01 00:00:00', strtotime($casual));
            $add_data = [];
            $num = 0;
            Db::name('dept_one')->where('clock',$month)->where("dept_id = $dept_id and type = 1")->delete();
            Db::name('dept_two')->where('clock',$month)->where("dept_id = $dept_id")->delete();
            if ($new_host_one) {
                foreach ($new_host_one as $k=>$v)
                {
                    // 获取一级平台的id
                    $exp = explode('-', $k);
                    $dept_one_name = $exp[0];
                    $dept_one_id = Db::name('dept_one')->insertGetId(['dept_id'=>$dept_id,'name'=>$dept_one_name,'type'=>1,'clock'=>$month,'createtime'=>strtotime(date('Y-m-d H:i:s'))]);
                    $add_data[$num]['dept_one_id'] = $dept_one_id;
                    $add_data[$num]['dept_id'] = $dept_id;
                    $add_data[$num]['name'] = $exp[1];
                    $add_data[$num]['alarms'] = $v;
                    $add_data[$num]['clock'] = $month;
                    $add_data[$num]['createtime'] = strtotime(date('Y-m-d H:i:s'));
                    $num += 1;
                }
                if ($add_data) {
                    $add = Db::name('dept_two')->insertAll($add_data);
                    if ($add) {
                        $first = 1;
                    }
                } else {
                    $first = 1;
                }
            }
            if ($other) {
                foreach ($other as $k=>$v)
                {
                    // 获取一级平台的id
                    $exp = explode('~', $k);
                    $dept_one_name = $exp[0];
                    $dept_one_id = Db::name('dept_one')->insertGetId(['dept_id'=>$dept_id,'name'=>$dept_one_name,'type'=>1,'clock'=>$month,'createtime'=>strtotime(date('Y-m-d H:i:s'))]);
                    $add_data[$num]['dept_one_id'] = $dept_one_id;
                    $add_data[$num]['dept_id'] = $dept_id;
                    $add_data[$num]['name'] = $exp[1];
                    $add_data[$num]['alarms'] = $v;
                    $add_data[$num]['clock'] = $month;
                    $add_data[$num]['createtime'] = strtotime(date('Y-m-d H:i:s'));
                    $num += 1;
                }
                if ($add_data) {
                    $add = Db::name('dept_two')->insertAll($add_data);
                    if ($add) {
                        $second = 1;
                    }
                } else {
                    $second = 1;
                }
            }
        } else {
            $first = 1;
            $second = 1;
        }

        if ($first && $second) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * 其他部门的业务接口类
     * */
    public function otherOneType($dept_id)
    {
        $data = Db::name('alarm_data')->where("dept_id = $dept_id and type = 1")->column('clock, name');
        $new = [];
        if ($data) {
            foreach ($data as $k=>&$v)
            {
                // 获取值出现的次数
                $ex = explode('-', $v);
                $v = $ex[3].'-'.$ex[4];
                $new = array_count_values($data);
            }
//            return $new;
            if ($new) {
                // 统计的月份
                $casual = array_keys($data)[0];
                $month = date('Y-m-01 00:00:00', strtotime($casual));
                $add_data = [];
                $num = 0;
                Db::name('dept_one')->where('clock',$month)->where("dept_id = $dept_id and type = 1")->delete();
                foreach ($new as $k=>$v)
                {
                    // 获取一级平台的id
                    $exp = explode('-', $k);
                    $dept_one_name = $exp[0];
                    $dept_one_id = Db::name('dept_one')->insertGetId(['dept_id'=>$dept_id,'name'=>$dept_one_name,'type'=>1,'clock'=>$month,'createtime'=>strtotime(date('Y-m-d H:i:s'))]);
                    $add_data[$num]['dept_one_id'] = $dept_one_id;
                    $add_data[$num]['dept_id'] = $dept_id;
                    $add_data[$num]['name'] = $exp[1];
                    $add_data[$num]['alarms'] = $v;
                    $add_data[$num]['clock'] = $month;
                    $add_data[$num]['createtime'] = strtotime(date('Y-m-d H:i:s'));
                    $num += 1;
                }
                if ($add_data) {
                    Db::name('dept_two')->where('clock',$month)->where("dept_id = $dept_id")->delete();
                    $add = Db::name('dept_two')->insertAll($add_data);
                    if ($add) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return true;
                }
            }
        } else {
            return true;
        }
    }

    /*
     * 服务应用类和底层服务环境类分析
     * */
    public function twoThreeType($dept_id, $type)
    {
        $data = Db::name('alarm_data')->where("dept_id = $dept_id and type = $type")->column('clock, name');
        $new = [];
        if ($data) {
            foreach ($data as $k=>&$v)
            {
                // 获取值出现的次数
                $v = explode('-', $v)[3];
                $new = array_count_values($data);
            }
            if ($new) {
                // 统计的月份
                $casual = array_keys($data)[0];
                $month = date('Y-m-01 00:00:00', strtotime($casual));
                $add_data = [];
                $num = 0;
                foreach ($new as $k=>$v)
                {
                    $add_data[$num]['dept_id'] = $dept_id;
                    $add_data[$num]['name'] = $k;
                    $add_data[$num]['alarms'] = $v;
                    $add_data[$num]['type'] = $type;
                    $add_data[$num]['clock'] = $month;
                    $add_data[$num]['createtime'] = strtotime(date('Y-m-d H:i:s'));
                    $num += 1;
                }
                if ($add_data) {
                    Db::name('dept_one')->where('clock',$month)->where("dept_id = $dept_id and type = $type")->delete();
                    $add = Db::name('dept_one')->insertAll($add_data);
                    if ($add) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return true;
                }
            }
        } else {
            return true;
        }
    }

    /*
     * 云分类
     * */
    public function cloud($dept_id)
    {
        $data = Db::name('alarm_data')->where("dept_id = $dept_id and type = 3")->column('clock, name');
        $new = [];
        if ($data) {
            foreach ($data as $k=>&$v)
            {
                // 获取值出现的次数
                // 获取值出现的次数
                $ex = explode('-', $v);
                $v = $ex[0].'-'.$ex[1];
                $new = array_count_values($data);
            }
//            return $new;
            if ($new) {
                // 统计的月份
                $casual = array_keys($data)[0];
                $month = date('Y-m-01 00:00:00', strtotime($casual));
                $add_data = [];
                $num = 0;
                foreach ($new as $k=>$v)
                {
                    // 获取云分类id
                    $cloud_name = explode('-', $k)[1];
                    $cloud_id = Db::name('cloud')->where('name',$cloud_name)->field('id')->find()['id'];
                    if (!$cloud_id) {
                        $cloud_id = Db::name('cloud')->insertGetId(['name'=>$cloud_name,'createtime'=>strtotime(date('Y-m-d H:i:s'))]);
                    }
                    $add_data[$num]['cloud_id'] = $cloud_id;
                    $add_data[$num]['name'] = $k;
                    $add_data[$num]['alarms'] = $v;
                    $add_data[$num]['clock'] = $month;
                    $add_data[$num]['createtime'] = strtotime(date('Y-m-d H:i:s'));
                    $num += 1;
                }
                if ($add_data) {
                    Db::name('cloud_alarms')->where('clock',$month)->where("cloud_id = $cloud_id")->delete();
                    $add = Db::name('cloud_alarms')->insertAll($add_data);
                    if ($add) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return true;
                }
            }
        } else {
            return true;
        }
    }

}
